desc "build tybc.rb"
file "tybc.rb" => "ruby/tybc.y" do |t|
  sh "racc -o #{t.name} #{t.prerequisites[0]}"
end

desc "build depro.exe"
task :depro => "depro.exe"

file "depro.exe" => ["depro/depro.d", "depro/bignum.d"] do |t|
  sh "dmd.exe -w -O -inline -release -of#{t.name} #{t.prerequisites.join(' ')}"
end

desc "compile all sources in sample/ by tybc.py"
task :sample_compile do
  samples=FileList.new("sample/*.txt")
  samples.each {|f|
    base=File.basename(f,".txt")
    sh "python tybc.py #{f} -o #{base}.code"
  }
end

desc "complile all sources in sample/ by tybc.rb"
task :sample_compile_r => :tybc do
  samples=FileList.new("sample/*.txt")
  samples.each {|f|
    base=File.basename(f,".txt")
    ruby "-w tybc.rb #{f} -o #{base}.code"
  }
end

desc "complile all sources in sample/ by ltybc.rb"
task :sample_compile_l do
  samples=FileList.new("sample/*.txt")
  samples.each {|f|
    base=File.basename(f,".txt")
    ruby "-w ruby/ltybc.rb #{f} -o #{base}.code"
  }
end

desc "run all *.code on eprovm.py"
task :runall do
  codes=FileList.new("*.code")
  codes.each {|f|
    sh "python eprovm.py #{f}"
  }
end

desc "run all *.code on depro"
task :runall_d => :depro do
  codes=FileList.new("*.code")
  codes.each {|f|
    sh "depro.exe #{f}"
  }
end

desc "run all *.code on eprovm.rb"
task :runall_r do
  codes=FileList.new("*.code")
  codes.each {|f|
    ruby "-w ruby/eprovm.rb #{f}"
  }
end

desc "remove all *.code"
task :clean do
  codes=FileList.new("*.code","*.obj","*.map")
  codes.each {|f|
    puts "del #{f}"
    File.unlink f
  }
end

desc "remove all files created"
task :clobber => :clean do
  list=FileList.new("tybc.rb","depro.exe").existing
  list.add("*.pyc","doc/*.html")
  list.each {|f|
    puts "del #{f}"
    File.unlink f
  }
  puts "rmtree release/"
  rmtree(["release/"])
end

desc "build documents"
task :doc => ["doc/tybc.html","doc/epro.html"]

rule '.html' => '.t2t' do |t|
  sh "txt2tags #{t.source}"
end

desc "make release archive"
task :release => [:depro,:doc] do
  puts "build release packgage to release/"
  mkdir_p "release/depro"
  mkdir_p "release/doc"
  mkdir_p "release/sample"
  mkdir_p "release/sample_tape"
  mkdir_p "release/misc"
  cp "depro.exe", "release"
  cp Dir.glob("*.py"), "release", {:preserve => true}
  cp Dir.glob("depro/*"), "release/depro", {:preserve => true}
  cp Dir.glob("doc/*.html"), "release/doc"
  cp Dir.glob("sample/*"), "release/sample", {:preserve => true}
  cp Dir.glob("sample_tape/*"), "release/sample_tape", {:preserve => true}
  cp Dir.glob("misc/*"), "release/misc", {:preserve => true}
end
